<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------

namespace app\common\validate;
use Think\Db;
/**
 * 基础验证器
 */
class Cart extends ValidateBase
{
	// 验证规则
    protected $rule = [
        'id'                => 'require|checkGoodsId',
        'quantity'          => 'require|>=:1|checkStock'
    ];

    // 验证提示
    protected $message = [
        'id.require'            => '商品必须',
        'quantity.require'      => '购买数量必须',
        'quantity.egt'          => '数量大于0'
    ];

    protected function checkGoodsId($value){
        if(Db::name("Goods")->where(["id"=>$value,"is_on_sale"=>1,"status"=>1,"is_delete"=>0])->count()==0){
            return "产品下架或不存在";
        }
        return true;
    }

    public function checkStock($value){
        if(Db::name("Goods")->where(["id"=>$value,"is_on_sale"=>1,"status"=>1,"is_delete"=>0,"stock"=>[">=","$value"]])->count()==0){
            return "产品下架或不存在";
        }
        return true;
    }

    // 应用场景
    protected $scene = [
        'addCart'  =>  ['id','quantity'],
    ];
}
